
//Namespace
package com.katujo.squirrel;

//Java imports
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Holds utility functionality that makes it easier to interact with the squirrel framework.
 * 
 * <br/><br/>
 * Copyright 2010 Katujo LTD<br/>
 * Licensed under the Apache License, Version 2.0<br/>
 * (the "License"); you may not use this file except in compliance with the License. You may<br/>
 * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required <br/>
 * by applicable law or agreed to in writing, software distributed under the License is distributed <br/>
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either <br/>
 * express or implied. See the License for the specific language governing permissions and <br/>
 * limitations under the License. <br/>
 * 
 * @author Johan Hertz
 *
 */
public class SquirrelUtil
{
	
	/**
	 * Create the parameters map form the data.
	 * <p>
	 * This is useful when using methods like the select(class, Map<String, Object>).
	 * Remember that the String in the map should be the column name as it appears in
	 * the database as Squirrel won't try to re-write column names for you.
	 * </p>
	 * <p>
	 * This uses a LinkedHashMap to keep the order in which the data was added. 
	 * </p>
	 * @param data
	 * @return
	 */
	public static Map<String, Object> param(Object ... data) throws Error
	{
		//Check that the data holds equal pairs
		if(data.length % 2 != 0)
			throw new Error("Data needs to hold pairs (uneven length)");
		
		//Create the map
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		
		//Create the key field
		String key = null;
		
		//Add the key value pairs to the map
		for(int i=0; i<data.length; i++)
		{
			//Key
			if(i%2 == 0)
				key = (String) data[i];
				
			//Value
			else map.put(key, data[i]);
		}
		
		//Return the map
		return map;
	}	

}
